/ Assembly List / LJCDataAccess / DataAccess / GetDataReader

Namespace - LJCDataAccess


Parameters
sql - The SQL command text.

Returns

A reference to the DbDataReader object.

Syntax

C#
public DbDataReader GetDataReader(String sql)

Executes a Select statement and retrieves the DbDataReader object. (E)

Remarks

It is the calling programs responsibility to close the connection when done.

Example

C#
// See the DataAccess setup code on the DataAccess class page.

// Executes a Select statement and retrieves the DbDataReader object.
private static void GetDataReader(DataAccess dataAccess)
{
  DbDataReader dbDataReader = null;

  string sql = "select * from TableName";
  try
  {
    // The "using" statement disposes the object when the scope is exited.
    using (dbDataReader = dataAccess.GetDataReader(sql))
    {
      while (dbDataReader.Read())
      {
        // Use the record here.
        for int index = 0; index < dbDataReader.FieldCount; index++)
        {
          string value = dbDataReader[index].ToString();
        }
      }
    }
  }
  finally
  {
    // The calling program must close the connection when done.
    dataAccess.CloseConnection();
  }
}

Copyright © Lester J. Clark and Contributors.
Licensed under the MIT License.